﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;

using Mvc.XForms.Controls.Formatters;
using Mvc.XForms.Controls.Settings;
using Mvc.XForms.Utilities;

namespace Mvc.XForms.Controls {
	public class Output<TContext> : CoreControlBase<TContext>, IOutput {
		private Expression<Func<TContext, object>> bind;

		public OutputSettings Settings { get; private set; }
		public string EncodedValue { get; set; }

		public override XFormControlType ControlType {
			get { return XFormControlType.Output; }
		}

		public Output(IContainerControl<TContext> parent, Expression<Func<TContext, object>> bind)
			: base(parent, string.Empty) {
			this.bind = bind;

			Settings = new OutputSettings(this);
		}

		protected override Func<object> SetValueAction {
			get { return () => bind.Compile()(Parent.ModelContext); }
		}

		public override string Render() {
			EncodedValue = Value == null ? string.Empty : HttpUtility.HtmlEncode(Value.ToString());
			return EncodedValue;
		}
	}
}